package cn.moukou.sbzz.service;

import cn.moukou.sbzz.mapper.AdminMapper;
import cn.moukou.sbzz.pojo.Admin;
import cn.moukou.sbzz.util.JwtUtil;
import cn.moukou.sbzz.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AdminService {

    @Autowired
    AdminMapper adminMapper;

    public Admin adminLogin(String uid, String pwd) throws Exception {
        Admin admin = adminMapper.selectById(uid);
        if(admin == null) {
            return null;
        }

        if (Md5Util.getMD5String(pwd).equals(admin.getPwd())) {
            Map<String, Object> tokenMap = new HashMap<>();
            tokenMap.put("uid", uid);
            tokenMap.put("role", "admin");
            String token = JwtUtil.generateToken(tokenMap, 6000);
            admin.setPwd(token);
            return admin;
        } else {
            return null;
        }
    }
}
